
package android.util;

import android.os.SystemProperties;


/**
 * A structure（结构） describing general information about a display,
 * such as its size, density, and font scaling.
 * 描述关于显示的一般信息的结构，例如其大小，密度和字体缩放。
 * <p>To access the DisplayMetrics members（c成员）, initialize an object like this:</p>
 * <pre> DisplayMetrics metrics = new DisplayMetrics();
 * getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre>
 */
public class DisplayMetrics {
    /**
     * Standard（标准） quantized（量化） DPI（每英寸点数） for low-density（低密度） screens.
     * density low
     */
    public static final int DENSITY_LOW = 120;

    /**
     * Standard quantized DPI for medium-density(中密度) screens.
     */
    public static final int DENSITY_MEDIUM = 160;

    /**
     * This is a secondary（次要，二次，代理） density, added for some common screen configurations.
     * It is recommended （被建议）that applications not generally target this as a first class density -- that is,
     *建议，应用程序通常不把它作为一级密度 - 也就是说，
     * don't supply specific graphics for this density, instead allow the platform to scale from other densities (typically {@link #DENSITY_HIGH}) as appropriate.
     *请勿为此密度提供特定的图形，而应允许平台根据需要从其他密度（通常为{@link #DENSITY_HIGH}）进行缩放。
     * In most cases (such as using bitmaps in {@link android.graphics.drawable.Drawable}) the platform can perform this scaling at load time,
     *在大多数情况下（例如在{@link android.graphics.drawable.Drawable}中使用位图），平台可以在加载时执行这个缩放，
     * so the only cost is some slight（轻微） startup runtime overhead（开支）.
     *所以唯一的成本是一些轻微的启动运行时间开销。
     * <p>This density was original（原本） introduced（被引进） to correspond（一致） with a 720p TV screen:
     * the density for 1080p televisions is {@link #DENSITY_XHIGH},
     * and the value here provides the same UI size for a TV running at 720p.
     * It has also found use in 7" tablets, when these devices have 1280x720 displays.
     */
    public static final int DENSITY_TV = 213;

    /**
     * Standard quantized DPI for high-density screens.
     */
    public static final int DENSITY_HIGH = 240;

    /**
     * Standard quantized DPI for extra-high-density（超高密度） screens.
     */
    public static final int DENSITY_XHIGH = 320;

    /**
     * Intermediate（中级） density for screens that sit somewhere between
     * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
     * This is not a density that applications should target, instead relying
     * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
     */
    public static final int DENSITY_400 = 400;

    /**
     * Standard quantized DPI for extra-extra-high-density（特超高密度） screens.
     */
    public static final int DENSITY_XXHIGH = 480;

    /**
     * Intermediate density for screens that sit somewhere between
     * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
     * This is not a density that applications should target, instead relying
     * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
     */
    public static final int DENSITY_560 = 560;

    /**
     * Standard quantized DPI for extra-extra-extra-high-density（超特超高密度） screens.
     * Applications should not generally worry（担心） about this density;
     * relying on XHIGH graphics being scaled up to it should be sufficient（足够的） for almost all cases.
     * 依靠XHIGH图形扩大到几乎所有的情况下应该是足够的。
     * A typical use of this density would be 4K television screens -- 3840x2160, which
     * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
     *
     * 这种密度的典型用法是4K电视屏幕 - 3840x2160，这是在DENSITY_XHIGH上运行的传统HD 1920x1080屏幕的2倍。
     */
    public static final int DENSITY_XXXHIGH = 640;

    /**
     * The reference（参照，引用） density used throughout the system.
     */
    public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;

    /**
     * Scaling factor to convert（转换） a density in DPI units to the density scale.
     * @hide
     */
    public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;

    /**
     * The device's density.
     * @hide because eventually（最终） this should be able to change while
     * running, so shouldn't be a constant（常量）.
     * @deprecated There is no longer a static density; you can find the
     * density for a display in {@link #densityDpi}.
     */
    @Deprecated
    public static int DENSITY_DEVICE = getDeviceDensity();

    /**
     * The absolute(绝对) width of the display in pixels.
     */
    public int widthPixels;
    /**
     * The absolute height of the display in pixels.
     */
    public int heightPixels;
    /**
     * The logical（合理的） density of the display.
     * This is a scaling factor for the Density Independent（独立） Pixel unit,
     * 这是“密度无关像素”单位的比例因子，
     * where one DIP is one pixel on an approximately（大约） 160 dpi screen (for example a 240x320, 1.5"x2" screen),
     * providing the baseline of the system's display.
     * Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.
     *  
     * <p>This value does not exactly（正确的） follow the real screen size (as given by
     * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
     * the overall UI in steps based on gross changes in the display dpi.
     *而是用于根据显示dpi的总体变化逐步缩放整个UI的大小。
     * For example, a 240x320 screen will have a density of 1 even if its width is
     * 1.8", 1.3", etc. However, if the screen resolution（分辨率） is increased to
     * 320x480 but the screen size remained 1.5"x2" then the density would be 
     * increased (probably to 1.5).
     *
     * @see #DENSITY_DEFAULT
     */
    public float density;
    /**
     * The screen density expressed as dots-per-inch.
     * 屏幕密度表示为每英寸点数
     * May be either
     * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
     */
    public int densityDpi;
    /**
     * A scaling factor for fonts displayed on the display.  This is the same
     * as {@link #density}, except（除了） that
     * it may be adjusted in smaller increments at runtime based on a user preference for the font size.
     * 可以根据用户对字体大小的偏好在运行时以较小的增量进行调整。
     */
    public float scaledDensity;
    /**
     * The exact physical pixels per inch of the screen in the X dimension.
     * X尺寸屏幕每英寸的确切物理像素。
     */
    public float xdpi;
    /**
     * The exact physical pixels per inch of the screen in the Y dimension.
     */
    public float ydpi;

    /**
     * The reported display width prior to any compatibility mode scaling being applied.
     * 在应用任何兼容模式缩放之前报告的显示宽度。
     * non compat(兼容)
     * @hide
     */
    public int noncompatWidthPixels;
    /**
     * The reported display height prior to any compatibility mode scaling
     * being applied.
     * @hide
     */
    public int noncompatHeightPixels;
    /**
     * The reported display density prior to any compatibility mode scaling being applied.
     * 在应用任何兼容模式缩放之前报告的显示密度。
     * @hide
     */
    public float noncompatDensity;
    /**
     * The reported display density prior to any compatibility mode scaling being applied.
     * 在应用任何兼容模式缩放之前报告的显示密度。
     * @hide
     */
    public int noncompatDensityDpi;
    /**
     * The reported scaled density prior to any compatibility mode scaling being applied.
     * 在应用任何兼容模式缩放之前报告的缩放密度。
     * @hide
     */
    public float noncompatScaledDensity;
    /**
     * The reported display xdpi prior to any compatibility mode scaling
     * being applied.
     * @hide
     */
    public float noncompatXdpi;
    /**
     * The reported display ydpi prior to any compatibility mode scaling
     * being applied.
     * @hide
     */
    public float noncompatYdpi;

    public DisplayMetrics() {
    }
    
    public void setTo(DisplayMetrics o) {
        widthPixels = o.widthPixels;
        heightPixels = o.heightPixels;
        density = o.density;
        densityDpi = o.densityDpi;
        scaledDensity = o.scaledDensity;
        xdpi = o.xdpi;
        ydpi = o.ydpi;
        noncompatWidthPixels = o.noncompatWidthPixels;
        noncompatHeightPixels = o.noncompatHeightPixels;
        noncompatDensity = o.noncompatDensity;
        noncompatDensityDpi = o.noncompatDensityDpi;
        noncompatScaledDensity = o.noncompatScaledDensity;
        noncompatXdpi = o.noncompatXdpi;
        noncompatYdpi = o.noncompatYdpi;
    }
    
    public void setToDefaults() {
        widthPixels = 0;
        heightPixels = 0;
        density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
        densityDpi =  DENSITY_DEVICE;
        scaledDensity = density;
        xdpi = DENSITY_DEVICE;
        ydpi = DENSITY_DEVICE;
        noncompatWidthPixels = widthPixels;
        noncompatHeightPixels = heightPixels;
        noncompatDensity = density;
        noncompatDensityDpi = densityDpi;
        noncompatScaledDensity = scaledDensity;
        noncompatXdpi = xdpi;
        noncompatYdpi = ydpi;
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
    }

    /**
     * Returns true if these display metrics equal the other display metrics.
     *
     * @param other The display metrics with which to compare.
     * @return True if the display metrics are equal.
     */
    public boolean equals(DisplayMetrics other) {
        return equalsPhysical(other)
                && scaledDensity == other.scaledDensity
                && noncompatScaledDensity == other.noncompatScaledDensity;
    }

    /**
     * Returns true if the physical aspects of the two display metrics
     * are equal.  This ignores the scaled density, which is a logical
     * attribute based on the current desired font size.
     *
     * @param other The display metrics with which to compare.
     * @return True if the display metrics are equal.
     * @hide
     */
    public boolean equalsPhysical(DisplayMetrics other) {
        return other != null
                && widthPixels == other.widthPixels
                && heightPixels == other.heightPixels
                && density == other.density
                && densityDpi == other.densityDpi
                && xdpi == other.xdpi
                && ydpi == other.ydpi
                && noncompatWidthPixels == other.noncompatWidthPixels
                && noncompatHeightPixels == other.noncompatHeightPixels
                && noncompatDensity == other.noncompatDensity
                && noncompatDensityDpi == other.noncompatDensityDpi
                && noncompatXdpi == other.noncompatXdpi
                && noncompatYdpi == other.noncompatYdpi;
    }

    @Override
    public int hashCode() {
        return widthPixels * heightPixels * densityDpi;
    }

    @Override
    public String toString() {
        return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
            ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
            ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
    }

    private static int getDeviceDensity() {
        // qemu.sf.lcd_density can be used to override ro.sf.lcd_density when running in the emulator, allowing for dynamic configurations.
        //在仿真器中运行时，qemu.sf.lcd_density可用于覆盖ro.sf.lcd_density，从而允许进行动态配置。
        // The reason for this is that ro.sf.lcd_density is write-once and is set by the init process when it parses build.prop before anything else.
        //原因是ro.sf.lcd_density是一次性编写的，在init之前解析build.prop的时候是由init进程设置的。
        return SystemProperties.getInt("qemu.sf.lcd_density",
                SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
    }
}
